package com.imay.live.manager;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.IMApplication;
import com.imay.live.R;
import com.imay.live.activity.family.FamilyCreateActivity;
import com.imay.live.activity.family.FamilyParams;
import com.imay.live.activity.family.helper.NimLogin;
import com.imay.live.model.UserExpsBean;
import com.imay.live.model.FamilyUserInfo;
import com.imay.live.model.FamilyExpsBean;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;

import java.util.Map;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by Administrator on 2017/9/8 0008.
 */

public class FamilyAccountManager {

    private FamilyUserInfo familyUserInfo;

    private static FamilyAccountManager instance;
    private Map<Integer, UserExpsBean> familyUserExps;
    private Map<Integer, FamilyExpsBean> familyExps;

    public static FamilyAccountManager getInstance() {
        if (instance == null) {
            instance = new FamilyAccountManager();
        }
        return instance;
    }

    public boolean isHasFamilyInfo() {
        return familyUserInfo != null && familyUserInfo.getFamily() != null && familyUserInfo.getFamily().getStatus() == FamilyCreateActivity.PASSING;
    }

    public boolean isHasFamilyUserInfo() {
        return familyUserInfo != null && familyUserInfo.getFamilyUser() != null;
    }

    /**
     * 设置家族信息
     *
     * @param familyUserInfo
     */
    public void setFamilyUserInfo(FamilyUserInfo familyUserInfo) {
        this.familyUserInfo = familyUserInfo;
    }

    /**
     * 清楚家族信息
     */
    public void clearFamilyUserInfo() {
        if (this.familyUserInfo != null) {
            this.familyUserInfo.clearFamilyUser();
        }
    }

    public void clearFamily() {
        if (this.familyUserInfo != null) {
            this.familyUserInfo.clearFamily();
        }
    }

    public void clear() {
        this.familyUserInfo = null;
    }

    /**
     * 获取家族个人信息
     *
     * @return
     */
    public FamilyUserInfo getFamilyUserInfo() {
        return familyUserInfo;
    }


    /**
     * 读取家族信息
     *
     * @param status 是否登录IM
     */
    public void loadFamilyUserInfo(final boolean status) {
        ProtoRequestHelper.task(ProtoHttpFactory.getImUserInfo()).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String stringCommand, String data) {
                Result<FamilyUserInfo> result = JSON.parseObject(data, new TypeReference<Result<FamilyUserInfo>>() {
                }.getType());
                if (result.result == CodeSuccess) {
                    familyUserInfo = result.data;
                    if (familyUserInfo.getFamilyUser() != null) {
                        if (status) {
                            loginNim();
                        }
                    }
                }
            }

            @Override
            public void onRequestError(String stringCommand, int code, String message) {

            }
        });
    }

    /**
     * 获取家族经验
     */
    public void loadExpConfig() {
        ProtoRequestHelper.task(ProtoHttpFactory.getExpConfig()).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String stringCommand, String data) {
                Result<String> result = JSON.parseObject(data, new TypeReference<Result<String>>() {
                }.getType());
                JSONObject jsonObject = JSON.parseObject(result.data);
                if (jsonObject.containsKey("familyUserExps")) {
                    String familyUserExpsStr = jsonObject.getString("familyUserExps");
                    if (!TextUtils.isEmpty(familyUserExpsStr)) {
                        familyUserExps = JSON.parseObject(familyUserExpsStr, new TypeReference<Map<Integer, UserExpsBean>>() {
                        }.getType());
                    }
                }

                if (jsonObject.containsKey("familyExps")) {
                    String familyExpsStr = jsonObject.getString("familyExps");
                    if (!TextUtils.isEmpty(familyExpsStr)) {
                        familyExps = JSON.parseObject(familyExpsStr, new TypeReference<Map<Integer, FamilyExpsBean>>() {
                        }.getType());
                    }
                }
            }

            @Override
            public void onRequestError(String stringCommand, int code, String message) {
            }
        });
    }

    public Map<Integer, UserExpsBean> getFamilyUserExps() {
        return familyUserExps;
    }

    public void setFamilyUserExps(Map<Integer, UserExpsBean> familyUserExps) {
        this.familyUserExps = familyUserExps;
    }

    public Map<Integer, FamilyExpsBean> getFamilyExps() {
        return familyExps;
    }

    public void setFamilyExps(Map<Integer, FamilyExpsBean> familyExps) {
        this.familyExps = familyExps;
    }

    /**
     * 网易登录
     */
    public void loginNim() {
        String account = familyUserInfo.getFamilyUser().getAccId();
        String token = familyUserInfo.getFamilyUser().getToken();
        if (NimLogin.getInstance().isLoginNim()) {
            NimLogin.getInstance().logout();
        }
        NimLogin.getInstance().loginNim(IMApplication.getInstance(), account, token);
    }

    /**
     * 获取account
     *
     * @return
     */
    public static String getAccount() {
        if (FamilyAccountManager.getInstance().isHasFamilyUserInfo()) {
            return FamilyAccountManager.getInstance().getFamilyUserInfo().getFamilyUser().getAccId();
        }
        return null;
    }

    /**
     * 家族是否被封
     *
     * @param status
     * @return
     */
    public static boolean isLocked(int status) {
        return status == FamilyParams.LOCKED;
    }

    /**
     * 获取职位
     *
     * @param admin
     * @return
     */
    public static int getAdminRstrId(int admin) {
        int strid = R.string.str_family_admin_0;
        switch (admin) {
            case FamilyParams.ADMIN_BIG:
                strid = R.string.str_family_admin_5;
                break;
            case FamilyParams.ADMIN_MID:
                strid = R.string.str_family_admin_1;
                break;
            case FamilyParams.ADMIN_SMALL:
                strid = R.string.str_family_admin_0;
                break;
        }
        return strid;
    }

    /**
     * 获取成员等级
     *
     * @param level
     * @return
     */
    public static String getLevelText(int level) {
        if (FamilyAccountManager.getInstance().getFamilyExps() != null
                && FamilyAccountManager.getInstance().getFamilyUserExps().containsKey(level)) {
            return FamilyAccountManager.getInstance().getFamilyUserExps().get(level).getTitle();
        }
        return "";
    }

    /**
     * 是否我的家族
     *
     * @param familyId
     * @return
     */
    public static boolean isMyFamily(int familyId) {
        if (FamilyAccountManager.getInstance().isHasFamilyUserInfo()) {
            return FamilyAccountManager.getInstance().getFamilyUserInfo().getFamilyUser().getFamilyId() == familyId;
        }
        return false;
    }

    /**
     * 返回我的职位
     *
     * @return
     */
    public static int getAdmin() {
        if (FamilyAccountManager.getInstance().isHasFamilyUserInfo()) {
            return FamilyAccountManager.getInstance().getFamilyUserInfo().getFamilyUser().getAdmin();
        }
        return -1;
    }

    /**
     * 家族是否被封禁
     *
     * @return
     */
    public static boolean isMyFamilyLocked() {
        if (FamilyAccountManager.getInstance().isHasFamilyInfo()) {
            return FamilyAccountManager.getInstance().getFamilyUserInfo().getFamily().getBeLocked() == FamilyParams.LOCKED;
        }
        return false;
    }
}
